package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import com.dudu.enumeration.StatusEnum;
import com.dudu.model.User;

import core.CoreDao;
import core.CoreDaoInvoke;
import core.db.DBConnectionUtil;

public class UserDao {


	public static void main(String[] args) {

		CoreDao coreDao = new CoreDaoInvoke();
		UserDao dao = new UserDao();

		User bean = new User();
		bean.setAccount("qinfx");
		bean.setEmail("qfx@yonyou.com");
		bean.setName("秦刚");
		bean.setPassword("123456");
		bean.setStatus(StatusEnum.ON.getKey());
		bean.setId(1);
		bean.setLast_login_time("2015-3-17 19:39:36");
		
		//dao.insert(coreDao, bean);
		//dao.updateLastLoginTime(coreDao, bean);
		dao.queryByAccount(coreDao, "qinfx");
		dao.queryById(coreDao, 1);
	}

	public int insert(CoreDao dao, User bean) {

		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_user(account, name, password, email, status, create_time, type, isv_id, isv_name)");
			sql.append("VALUES(?, ?, ?, ?, ?, sysdate(),?, ?, ? )");

			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { 
						bean.getAccount(), bean.getName(), bean.getPassword(), bean.getEmail(), bean.getStatus(),
						bean.getType(), bean.getIsv_id(), bean.getIsv_name()					
					});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	public int update(CoreDao dao, User bean) {
		Connection con = null;
		int result = -1;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			
			StringBuffer sql = new StringBuffer(
					"UPDATE t_user set name=?, email=?, status=?, type=?, isv_id=?, isv_name=?");
			sql.append(" where id=?");

			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] {
						bean.getName(), bean.getEmail(), bean.getStatus(),
						bean.getType(), bean.getIsv_id(), bean.getIsv_name(),
						bean.getId()
					}

			);

			if (list != null && list.size() > 0) {
				list.get(0);
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	/**
	 * 更新用户登录信息数据
	 * @param dao
	 * @param bean 用户的实体类数据
	 * @return
	 */
	public int updateLastLoginTime(CoreDao dao, User bean) {
		Connection con = null;
		int result = -1;

		try {
			//获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			//拼写sql
			StringBuffer sql = new StringBuffer(
					"UPDATE t_user set login_time=?, last_login_time=?, ip=?");
			sql.append(" where id=?");
			//执行sql
			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { bean.getLogin_time(), 
						bean.getLast_login_time(), bean.getIp(), bean.getId() }

			);
			
			if (list != null && list.size() > 0) {
				list.get(0);
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public int updatePwd(CoreDao dao, User bean) {
		Connection con = null;
		int result = -1;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			
			StringBuffer sql = new StringBuffer(
					"UPDATE t_user set password=?");
			sql.append(" where id=?");

			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { 
							bean.getPassword(), bean.getId() 
						}

			);

			if (list != null && list.size() > 0) {
				list.get(0);
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public User queryByAccount(CoreDao dao, String account) {
		Connection con = null;
		User result = null;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");

			StringBuffer sql = new StringBuffer(
					"select id, account, name, password, email, status, create_time, login_time, last_login_time, type, isv_id, isv_name FROM t_user where account=?");
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { account });
			if (list != null && list.size() > 0) {
				List<User> items = dao.covertToBean(list, User.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public User queryById(CoreDao dao, int id) {

		Connection con = null;
		User result = null;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");

			StringBuffer sql = new StringBuffer(
					"select id, account, name, password, email, status, create_time, login_time, " +
					" last_login_time, type, isv_id, isv_name FROM t_user where id=?");
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { id });
			if (list != null && list.size() > 0) {
				List<User> items = dao.covertToBean(list, User.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public List<JSONObject> queryAll(CoreDao dao) {

		Connection con = null;
		List<JSONObject> result = null;
		
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select * from t_user order by create_time desc ");
			result = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { null });

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
}
